
package com.monlyu.tag;

import java.io.IOException;

import javax.servlet.jsp.tagext.TagSupport;

import ognl.NoSuchPropertyException;
import ognl.Ognl;
import ognl.OgnlException;

import org.apache.struts2.views.jsp.TagUtils;

import com.monlyu.util.LogUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.util.WebOptUtil;

/********************************************************************************
 * <b>功能描述☞</b>其他标签的基础类.任何自定义的标签类均通过此标签库进行继承扩展<br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>monlyu<br>
 * <b>文件路径☞</b>com.monlyu.tag.BaseTag.java<br>
 * <b>开发日期☞</b>2010-1-14 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.5
 ********************************************************************************/
public abstract class BaseTag extends TagSupport {

    /** common文件夹的路径 */
    protected final String COMMON_WEBDIR = WebOptUtil.APP_ARGS.getWebRelUrl() + "common/";
    protected final String COMMON_SCRIPT_DIR = WebOptUtil.APP_ARGS.getWebRelUrl() + "common/scripts/";
    protected final String COMMON_CSS_DIR = WebOptUtil.APP_ARGS.getWebRelUrl() + "common/css/";
    protected final String COMMON_IMAGES_DIR = WebOptUtil.APP_ARGS.getWebRelUrl() + "common/images/";

    public abstract int doStartTag();

    public void write2Page(Object input){
        try {
            if (!StringUtil.isTrimEmpty(input)) {
                pageContext.getOut().write(input.toString());
            }
        }
        catch (IOException e) {
            LogUtil.log(BaseTag.class, e).error("把标签内容输出到页面时发生错误\n内容" + input, e);
        }
    }

    protected Object getValueFromValueStack(String proname){
        Object object = null;
        try {
            final Object bean = TagUtils.getStack(pageContext).peek();
            object = Ognl.getValue(proname, bean);
        }
        catch (NoSuchPropertyException e) {
            // LogUtil.log(BaseTag.class, e).info("无法从堆栈中获取值", e);
        }
        catch (OgnlException e) {
            // LogUtil.log(BaseTag.class, e).info("无法从堆栈中获取值", e);
        }
        return object;
    }

    /**
     * 获取非对象值
     * 
     * @param name
     * @return
     */
    protected Object getStatckValue(String name){
        return TagUtils.getStack(pageContext).findValue(name);
    }
}
